package com.exrobot.module.rbt.mq.consumer.deliveryFood;

import com.alibaba.fastjson.JSON;
import com.exrobot.framework.common.util.date.LocalDateTimeUtils;
import com.exrobot.module.rbt.controller.admin.order.vo.OrderPageReqVO;
import com.exrobot.module.rbt.controller.admin.orderitem.vo.OrderItemPageReqVO;
import com.exrobot.module.rbt.controller.admin.robottask.vo.RobotTaskSaveReqVO;
import com.exrobot.module.rbt.controller.app.vo.MiniproMessageVO;
import com.exrobot.module.rbt.dal.dataobject.order.OrderDO;
import com.exrobot.module.rbt.dal.dataobject.orderitem.OrderItemDO;
import com.exrobot.module.rbt.dal.dataobject.robotinfo.RobotInfoDO;
import com.exrobot.module.rbt.dal.dataobject.robottask.RobotTaskDO;
import com.exrobot.module.rbt.enums.RbtOrderStatusEnum;
import com.exrobot.module.rbt.enums.RbtTaskTypeEnum;
import com.exrobot.module.rbt.framework.util.CommonUtils;
import com.exrobot.module.rbt.mq.message.deliveryFood.DeliveryFoodMessage;
import com.exrobot.module.rbt.mq.message.order.OrderMessage;
import com.exrobot.module.rbt.service.canteeninfo.CanteenInfoService;
import com.exrobot.module.rbt.service.mqtt.MqttMessageService;
import com.exrobot.module.rbt.service.order.OrderService;
import com.exrobot.module.rbt.service.robotScheduler.RobotSchedulerService;
import com.exrobot.module.rbt.service.robottask.RobotTaskService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 针对 {@link OrderMessage} 的消费者
 *
 * @author wf
 */
@Component
@Slf4j
public class DeliveryFoodSendConsumer {

    @Resource
    private OrderService orderService;

    @Resource
    private RobotTaskService taskService;

    @Resource
    private RobotSchedulerService schedulerService;

    @Resource
    private MqttMessageService mqttMessageService;

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(DeliveryFoodMessage deliveryFoodMessage) {
        log.info("[RedisMQ-取餐完成]接收消息内容：{}", JSON.toJSONString(deliveryFoodMessage));
        OrderDO orderDO = orderService.updateOrderCompleted(deliveryFoodMessage.getSn(),deliveryFoodMessage.getCode());
        if(orderDO != null) {
            if(orderDO.getOrderType().equals(1)) {
                MiniproMessageVO messageVO = new MiniproMessageVO();
                messageVO.setOrderSn(orderDO.getOrderSn());
                messageVO.setContent("完成");
                mqttMessageService.sendMiniproMessage(messageVO);
            }
//            unassignedOrders();
        }
    }
    //未分配的订单查看分配
    private void unassignedOrders() {
        OrderItemPageReqVO reqVO = new OrderItemPageReqVO();
        reqVO.setStatus(RbtOrderStatusEnum.WAIT.getVal());
        reqVO.setCreateTime(LocalDateTimeUtils.getTodayRange());
        List<OrderItemDO> itemDOList = orderService.getOrderItemList(reqVO);
        if(itemDOList == null || itemDOList.isEmpty()) {
            return;
        }
        OrderPageReqVO orderPageReqVO = new OrderPageReqVO();
        orderPageReqVO.setOrderSns(itemDOList.stream().map(OrderItemDO::getOrderSn).collect(Collectors.toSet()));
        List<OrderDO> orderDOList = orderService.getOrderList(orderPageReqVO);
        if(orderDOList == null || orderDOList.isEmpty()) {
            return;
        }
        for(OrderDO orderDO : orderDOList) {
            List<OrderItemDO> orderItemDOList = itemDOList.stream().filter(w->w.getOrderSn().equals(orderDO.getOrderSn())).toList();
            RobotInfoDO robotInfoDO = schedulerService.orderMatchRobot(CommonUtils.getFloorByPoint(orderDO.getPoint()),orderItemDOList);
            if(robotInfoDO == null) {
                return;
            }
            RobotTaskSaveReqVO saveReqVO = new RobotTaskSaveReqVO();
            saveReqVO.setTaskType(RbtTaskTypeEnum.DELIVERY.getTypeVal());
            saveReqVO.setPoint(orderDO.getPoint());
            saveReqVO.setRobotSn(robotInfoDO.getSn());
            RobotTaskDO taskDO = taskService.createRobotTask(saveReqVO);
            if(taskDO == null) {
                continue;
            }
            orderService.updateOrderItemTask(orderItemDOList.stream().map(OrderItemDO::getId).toList(),taskDO.getTaskNo(),robotInfoDO.getSn(),RbtOrderStatusEnum.TAKE.getVal());
        }
    }

}
